package com.wzy.service.impl;

import com.wzy.entity.Blog;
import com.wzy.entity.Comments;
import com.wzy.entity.Hero;
import com.wzy.mapper.BlogMapper;
import com.wzy.mapper.CommentsMapper;
import com.wzy.mapper.HeroMapper;
import com.wzy.service.CommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2021-02-09
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {
    @Resource
    private CommentsMapper commentsMapper;
    @Resource
    private BlogMapper blogMapper;
    @Resource
    private HeroMapper heroMapper;

    //存放迭代找出的所有子代的集合
    private List<Comments> tempReplys = new ArrayList<>();

    @Override
    public int append(Comments comments) {
        //        如果parent_commentsId！=-1的话就说明他有父亲
        long parentCommentsId=comments.getParentCommentsId();
        if(parentCommentsId!=-1){
            comments.setParentComment(this.commentsMapper.findByParentsId(parentCommentsId));
        }else{
            comments.setParentComment(null);
        }
        //        增加博客里面的评论数
        if(comments.getBlog()!=null&&comments.getBlog().getBlogId()!=0){
            Blog blog=this.blogMapper.selectById(comments.getBlog().getBlogId());
            blog.setCommentCount(blog.getCommentCount()+1);
            blogMapper.update(blog);
            comments.setHeroId((long)0);
        }else{
            Hero hero=this.heroMapper.selectById(comments.getHeroId());
            hero.setCommentCount(hero.getCommentCount()+1);
            heroMapper.update(hero);
            comments.setBlogId((long)0);
        }

        return this.commentsMapper.insert(comments);
    }

    @Override
    public List<Comments> listCommentsByBlogId(Long blogId,Long heroId) {
        //  第一步查询顶级评论
        List<Comments> comments=this.commentsMapper.findCommentsByBlogIdAndNotParent(blogId,heroId);
//        遍历每一个顶级评论 为了找出每一个的回复
        for (Comments comment : comments) {
            long topCommentsId=comment.getCommentsId();
            String parentNickname=comment.getNickname();
//            一级回复的储存集合
            List<Comments> firstReply=this.commentsMapper.findFirstReply(blogId,heroId,topCommentsId);
//            查询一级回复
            combineFirstReply(blogId,heroId,firstReply,parentNickname);
//            设置顶级评论的回复集合
            comment.setReplyComments(tempReplys);
//            遍历结束将tempReplys初始化
            tempReplys=new ArrayList<>();
        }

        return comments;
    }

    private void combineFirstReply(long blodId,long heroId,List<Comments> firstReply,String parentNickname){
        if(firstReply.size()>0){
            for (Comments firstComment : firstReply) {
                long firstCommentsId=firstComment.getCommentsId();
                String firstNickname=firstComment.getNickname();
//                设置一级回复的父昵称
                firstComment.setParentNickname(parentNickname);
//                firstComment.setNickname(firstNickname);
                tempReplys.add(firstComment);
//                查询二级回复
                combineSecondReply(blodId,heroId,firstCommentsId,firstNickname);
            }
        }
    }


    private void combineSecondReply(long blodId,long heroId, long firstCommentsId,String parentNickname){
//      根据一级回复的id找到二级回复
        List<Comments> secondReply=this.commentsMapper.findSecondReply(blodId,heroId,firstCommentsId);
        if(secondReply.size()>0){
            for (Comments secondComment : secondReply) {
                long secondCommentsId=secondComment.getCommentsId();
                String secondNickname=secondComment.getNickname();
//                设置一级回复的父昵称
                secondComment.setParentNickname(parentNickname);
                secondComment.setNickname(secondNickname);
                tempReplys.add(secondComment);
//                查询二级回复
                combineSecondReply(blodId,heroId,secondCommentsId,secondNickname);
            }
        }
    }
}
